package com.ermax.aircond.maintain.web.bean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ermax.aircond.common.domain.UserGroup;
import com.ermax.aircond.maintain.service.MaintainService;
import com.ermax.aircond.resource.MessageBundle;
import com.ermax.aircond.util.web.bean.DataTableBean;

@Scope("view")
@Component
public class UserGroupMaintainBean extends DataTableBean implements java.io.Serializable{

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private MaintainService maintainService;	
	@ManagedProperty(value = "#{loginBean.sessionUser}")
	private String sessionUser;	
		
	private UserGroup editedItem;
	private List<UserGroup> userGroups = new ArrayList<UserGroup>();	
	
	@PostConstruct
	public void init() {
		try{						
			reset();
		}catch(Exception ex){
			String message = MessageBundle.getInstance().getMessage("error_init_fail", FacesContext.getCurrentInstance().getViewRoot().getLocale()); 
			FacesMessage facesMessage  = new FacesMessage(message);
			FacesContext.getCurrentInstance().addMessage(null,facesMessage);
		}
	}
	
	private void reset() throws Exception {
		userGroups = maintainService.getAllUserGroups();		
		editedItem = new UserGroup();
	}
	
	public void create(){
		try{							
			maintainService.createUserGroup(editedItem, sessionUser);
			reset();
		}catch(Exception ex){
			String message = MessageBundle.getInstance().getMessage("error_create_fail", FacesContext.getCurrentInstance().getViewRoot().getLocale()); 
			FacesMessage facesMessage  = new FacesMessage(message);
			FacesContext.getCurrentInstance().addMessage(null,facesMessage);
		}
	}
	
	public void update(){
		try{			
			maintainService.updateUserGroup(editedItem, sessionUser);
			reset();
		}catch(Exception ex){
			String message = MessageBundle.getInstance().getMessage("error_update_fail", FacesContext.getCurrentInstance().getViewRoot().getLocale()); 
			FacesMessage facesMessage  = new FacesMessage(message);
			FacesContext.getCurrentInstance().addMessage(null,facesMessage);
		}
	}
	
	public void remove(){
		try{
			UserGroup userGroup = userGroups.get(getCurrentIndex());
			maintainService.deleteUserGroup(userGroup);
			reset();
		}catch(Exception ex){
			String message = MessageBundle.getInstance().getMessage("error_remove_fail", FacesContext.getCurrentInstance().getViewRoot().getLocale()); 
			FacesMessage facesMessage  = new FacesMessage(message);
			FacesContext.getCurrentInstance().addMessage(null,facesMessage);
		}
	}
	
	public void search() {
		try{
			userGroups = maintainService.getUserGroups(this.getSearchStr());
		}catch(Exception ex){}
	}	

	public MaintainService getMaintainService() {
		return maintainService;
	}

	public void setMaintainService(MaintainService maintainService) {
		this.maintainService = maintainService;
	}

	public UserGroup getEditedItem() {
		return editedItem;
	}

	public void setEditedItem(UserGroup editedItem) {
		this.editedItem = editedItem;
	}

	public List<UserGroup> getUserGroups() {
		return userGroups;
	}

	public void setUserGroups(List<UserGroup> userGroups) {
		this.userGroups = userGroups;
	}
	
	
}
